<?php defined('SYSPATH') OR die('No direct access allowed.');

class Media_Model extends ORM{
 
	protected $has_many=array('contents'=>'contmedlinks'); 
	
	var $varsarray=array(''=>'None', 'border'=>'Has Border');
	
	
	public function before_delete(){
		if(is_file($this->url)){
			unlink($this->url);
		}
	}
	
	public function get_icon($w, $h){
		switch($this->type){
			case "image":
				return $this->get_resized_url($w, $h);
				break;
			case "pdf":
				return "mediaadmin/img/doc_types/icon_pdf.gif";
				break;
			case "movie":
				return "mediaadmin/img/doc_types/icon_film.gif";
				break;
			case "zip":
				return "mediaadmin/img/doc_types/icon_zip.gif";
				break;
			default :
				return "mediaadmin/img/doc_types/icon_generic.gif";
			
		}
	}
	
	public function get_resized_url($w, $h, $url=NULL){
		$newurl='';	
		$origurl=$this->url;	
		if($url!=NULL){
			$origurl=$url;
		}
		switch($this->type){
			case 'video':
				$origurl=$this->stillurl;
				if($this->stillurl!=''){
					$origurl=$this->stillurl;
				}else{
					$origurl='media/img/fileicons/pdf_icon.jpg';
				}
				break;
			case 'pdf':
				if($this->stillurl!=''){
					$origurl=$this->stillurl;
				}else{
					$origurl='media/img/fileicons/pdf_icon.jpg';
				}
				break;
			case 'zip':
				if($this->stillurl!=''){
					$origurl=$this->stillurl;
				}else{
					$origurl='media/img/fileicons/pdf_icon.jpg';
				}
				break;		
		}
		
		$isimage=substr(file::mime($this->url),0,5)=='image';	
		$isvideo=($this->extension=='flv' || ($this->extension=='mp4'));
		if($isvideo){
			$origurl=$this->stillurl;
		}
		
	
		if(file_exists($origurl)){		
			$patharray=split('/', $origurl);
			$fn=$patharray[count($patharray)-1];
			$newurl='media/resized/res_'.$w.'_'.$h.'_'.$fn;		
			if( ! file_exists($newurl))
			{
				$img = new Image($origurl);		
				$img->resize($w,$h,Image::AUTO);
				$img->save($newurl);
			}		
		}		
		return $newurl;			
	}
	
	
	public function get_resized_url_shineout($w, $h,$sw=10, $sh=10, $url=NULL){
		
		if(file_exists($this->url)){		
			$patharray=split('/', $this->url);
			$fn=$patharray[count($patharray)-1];
			$newurl='media/resized/res_shine_'.$w.'_'.$h.'_'.$fn;		
			
			if( ! file_exists($newurl))
			{
				$img = new Image($this->url);		
				$img->resize($w-2*$sw, $h-2*$sh,Image::AUTO);
				$img->save($newurl);
				$img = new Image($newurl);
				$img->add_shineout($sw,$sh);
			}		
		}		
		
		return $newurl;		
		
	}
	
	
	public function get_tinted_url($w, $h, $color, $url=NULL){
		
		$newurl='';
		
		$origurl=$this->url;	
		
		if($url!=NULL){
			$origurl=$url;
		}
		$isimage=substr(file::mime($this->url),0,5)=='image';	
		
		$isvideo=($this->extension=='flv' || ($this->extension=='mp4'));
		if($isvideo){
			$origurl=$this->stillurl;
		}
		
		$ispdf=($this->extension=='pdf');
		if($ispdf){
			$origurl='media/testurl.jpg';
		}
	
		if(file_exists($origurl)){		
			$patharray=split('/', $origurl);
			$fn=$patharray[count($patharray)-1];
			
			$newurl='media/resized/res_'.$w.'_'.$h.'_'.$color.'_'.$fn;
						
			if( ! file_exists($newurl))
			{	
				$img = new Image($origurl);
				
				if($w!=0 && $h!=0){
					$img->resize($w,$h,Image::AUTO);
				}
				$img->save($newurl);
				$img = new Image($newurl);
				$img->colorize($color);			
			}
		}
		return $newurl;		
	}
	

	////////////////////////////////////////////////////// UPLOAD
	
	public function upload_multiple_files($fieldname){
		$files_orig=$_FILES[$fieldname];
		
		$numfiles=count($files_orig['name']);
		$filearray=array();
		$count=0;
		for ($i=0;$i<$numfiles ;$i++){
			if($files_orig['name'][$i]!=''){
				foreach($files_orig as $key=>$value){
					$filearray[$count][$key]=$value[$i];
				}
				$count++;
			}
		}
		
		foreach ($filearray as & $filedata){		
			$filedata['url'] = upload::save($filedata);
			$extarray=explode('.',$filedata['name']);
			$filedata['extension']=$extarray[count($extarray)-1];
			$filedata['mimetype']=file::mime($filedata['url']);
			$filedata['type']=$this->check_type($filedata['extension'], $filedata['mimetype']);
			$filedata['origfilename']=$filedata['name'];
		}
		
		return $filearray;
	}
	
	
	public function upload_file( $fieldname='file' ){
			
			$filedata=array();		
			$url=upload::save($fieldname);
			
			if($url!=NULL){
				$filedata['url'] = $url;
				$filedata['mimetype']= file::mime($filedata['url']);
				$extarray=explode('.',$filedata['url']);
				$filedata['extension']=$extarray[count($extarray)-1];
				$filedata['type']=$this->check_type($filedata['extension'], $filedata['mimetype']);
								
				$this->url=$filedata['url'];
				$this->mimetype=$filedata['mimetype'];
				$this->extension=$filedata['extension'];
				$this->type=$filedata['type'];
				$this->origfilename=$_FILES[$fieldname]['name'];
				
				
				return $filedata;
			}
		
			return NULL;
	}
	
	
	////////////////////////////////////////////////////// TYPECHECK
	
	public function check_type($ending, $mime){
	
		$type='';
		$ending=strtoupper($ending);
		
		if(substr($mime,0,5)=='image'){
			return 'image';
		}
		
		if($ending=='FLV' || $ending=='MPG' || $ending=="MP4"){
			return 'movie';
		}
		
		if($mime=='application/pdf' || $ending=='PDF'){
			return 'pdf';
		}
		
		if($ending=='ZIP'){
			return 'zip';
		}
		
		
		
		
		return $type;
	}
	
}